package com.aliang.frame.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.aliang.frame.domain.Role;
import com.aliang.frame.domain.User;
import com.aliang.frame.domain.enums.AuthEnum;
import com.aliang.frame.exception.BusinessException;
import com.aliang.frame.repository.RoleMapper;
import com.aliang.frame.service.RoleService;
import com.aliang.frame.service.enums.ActionEnum;
import com.aliang.frame.utility.ModelMapperSingle;
import com.aliang.frame.viewmodel.admin.role.RolePageRequestVM;
import com.aliang.frame.viewmodel.admin.role.RolePageResponseVM;
import com.aliang.frame.viewmodel.admin.role.RoleRequestVM;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Transactional
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {

    protected final static ModelMapper modelMapper = ModelMapperSingle.Instance();
    private final RoleMapper roleMapper;

    public RoleServiceImpl(RoleMapper roleMapper) {
        super(roleMapper);
        this.roleMapper = roleMapper;
    }

    @Override
    public PageInfo<Role> page(RolePageRequestVM requestVM) {
        return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "role_sort asc").doSelectPageInfo(() ->
                roleMapper.page(requestVM)
        );
    }

    @Override
    public void edit(RoleRequestVM model, User user) {
        if (model.getMenuIds().contains(AuthEnum.SUPER_ADMIN.getCode())) {
            throw new BusinessException("超级管理员无法被修改或创建");
        }
        ActionEnum actionEnum = (model.getRoleId() == null) ? ActionEnum.ADD : ActionEnum.UPDATE;
        String menus = model.getMenuIds().stream().map(String::valueOf).collect(Collectors.joining(","));
        Role role = null;
        if (actionEnum == ActionEnum.ADD) {
            Date now = new Date();
            role = modelMapper.map(model, Role.class);
            role.setMenus(menus);
            role.setCreateBy(user.getId());
            role.setCreateTime(now);
            role.setUpdateBy(user.getId());
            role.setUpdateTime(now);
            role.setDeleted(false);
            role.setStatus(true);
            role.setAdmin(false);
            roleMapper.insertSelective(role);
        } else {
            role = roleMapper.selectByPrimaryKey(model.getRoleId());
            String[] split = role.getMenus().split(",");
            if (Arrays.stream(split).anyMatch(String.valueOf(AuthEnum.SUPER_ADMIN.getCode())::equals)) {
                throw new BusinessException("超级管理员无法被修改或创建");
            }
            Date now = new Date();
            modelMapper.map(model, role);
            role.setMenus(menus);
            role.setUpdateTime(now);
            role.setUpdateBy(user.getId());
            roleMapper.updateByPrimaryKeySelective(role);
        }
        model.setRoleId(role.getRoleId());
    }

    @Override
    public RolePageResponseVM roleToVM(Integer id) {
        Role role = roleMapper.selectByPrimaryKey(id);
        if (role == null) {
            return null;
        }
        RolePageResponseVM vm = new RolePageResponseVM(role);
        return vm;
    }

    @Override
    public int updateByIdFilter(Role record) {
        return super.updateByIdFilter(record);
    }

    @Override
    public List<Role> selAllRoleName() {
        return roleMapper.selAllRoleName();
    }
}
